<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script language="javascript" src="../script/common.js"
	type="text/javascript"></script>
<meta name="author" content="liunan">
<title>机构组管理</title>
<script type="text/javascript">	
	var org = Common.cOrg();	//当前单位	
	var qt = {};				//数据表格对象
	var sqt = null
	var sg = {
		dataMap : {},
		adds : {},
		dels : {}
	}
	function load(){
		Eui.showNavigation();
		setPages(["listview","detview","detviewdiv"]);
		var org = Common.cOrg();
		searchForm.orgName.value = org.name;
		var orglist = [];
		var orgs = Common.getItems("org_level");
		for (var i=0;i<orgs.length;i++){
			var o = orgs[i];
			if (org.orgLevel < o.code)
				orglist.push(o)
		}
		Common.initDict(orgGroupForm.orgLevel,orglist,"请选择")
		createTable();
	}
	function createTable(){
		var columns = [];
		columns[0]={type:"checkbox"} 
		columns[1]={render:function(v,r){
			return "<a href=# onclick=\"onView("+r[0]+");return false;\">"+v+"</a>";	
		}}
		columns[6]={title:"操作",width:"15%",render:function(v,r){
			var str = ""
			str += "<a href=# onclick='onEdit("+r[0]+")'>编辑</a>"
			return str
		},align:"center",order:false}
		qt = new QueryTable("orggroup_query","searchForm","datalist",columns,{
			buttons:[
				{value:"新增组",actions:onNew},
				{value:"删除组",actions:onDelete}
			]
		});
	}
	function createStationQt(){
		if (!sqt){
			var columns = [];
			columns[0]={title : "ID",type:"checkbox"} 
			columns[1]={title:"机构编号"} 
			columns[2]={title:"机构名称"}
			sqt = new QueryTable(null,null,"orggroupdiv",columns,{
				title:"成员机构列表",
				columnsConf:false,
				buttons:[
					{value:"添加机构",actions:addStations},
					{value:"删除机构",actions:delStations}
				]
			});
		}
	}
	function showStationQt(){
		sqt.showTable()
		sg.dataMap= {} 
		for (var i=0;i<sqt.dataSrc.objects.length;i++){
			var s = sqt.dataSrc.objects[i]
			sg.dataMap[s[0]] = s
		}
	}
	function showData(){	
		qt.showTable();
	}
	function onView(id){
		DWR.call("partyService.getOrgGroup",id,function(data){
			var group = data.group;
			var members = data.members;
			var strs = "";
			for (var i=0;i<members.length;i++){
				var s = members[i];
				if (strs) strs += ",";
				strs += s[2];
			}
			$("orgGroupName").innerHTML = group.name;
			$("orgLevelText").innerHTML = Common.getText("org_level",group.orgLevel)
			$("memberNames").innerHTML = strs;
			switchPage("detviewdiv");
		},{blockScreen:true})
	}
	function onNew(){
		orgGroupForm.reset();
		orgGroupForm.orgLevel.disabled = false;
		orgGroupForm.orgLevel.className = "";
		switchPage("detview");
		createStationQt()
		sqt.dataSrc = []
		showStationQt()
	}
	function onEdit(id){
		orgGroupForm.orgLevel.disabled = true;
		orgGroupForm.orgLevel.className = "readonly";
		DWR.call("partyService.getOrgGroup",id,function(data){
			var group = data.group;
			var stations = data.members;
			Common.setValues("orgGroupForm",group)
			switchPage("detview");
			createStationQt()
			sqt.dataSrc = stations
			showStationQt()
		},{blockScreen:true})
	}
	function addStations(){
		var orgLevel = orgGroupForm.orgLevel.value;
		if (!orgLevel){showWarning("请选择组的机构等级！");return;}
		SystemMgnt.selOrgList(function(ss){
			var arrs = sqt.dataSrc.objects
			for (var i=0;i<ss.length;i++){
				var s = ss[i];
				if (!sg.dataMap[s.id]){
					arrs.push([s.id,s.uniqueId,s.name])
					if (!sg.dels[s.id])
						sg.adds[s.id] = s
					else
						sg.dels[s.id] = null
				}
			}
			sqt.dataSrc = arrs
			showStationQt()
		},{onlyChildren : true ,orgLevel : orgLevel})
	}
	function delStations(){
		var ids = sqt.getBoxValue();
		if (!ids) {showWarning("请选择一个机构！");return;};
		var ids = "," + ids + ",";
		var arrs = sqt.dataSrc.objects;
		var dataSrc = []
		for (var i=0;i<arrs.length;i++){
			var s = arrs[i];
			if (ids.indexOf(","+s[0]+",") ==-1 )
				dataSrc.push(s);
			else{
				if (!sg.adds[s[0]])
					sg.dels[s[0]] = s
				else
					sg.adds[s[0]] = null
			}
		}
		sqt.dataSrc = dataSrc
		showStationQt()
	}
	
	function saveOrgGroup(){
		if (!checkForm(orgGroupForm)) return;
		var sgObj = Common.getValues("orgGroupForm");
		var adds = toArray(sg.adds)
		var dels = toArray(sg.dels)
		DWR.call("partyService.saveOrgGroup",sgObj,adds,dels,function(){
			showInformation("机构组"+sgObj.name+"保存成功");
			back()
		},{blockScreen:true})
	}
	function toArray(map){
		if (typeof map != "object") return [];
		var arrs = []
		for (prop in map){
			var c = map[prop]
			if (!c || typeof c == "function") continue;
			arrs.push(prop)
		}
		return arrs;
	}
	function onDelete(){
		var ids = qt.getBoxValue();
		if(!ids){showInformation("请选择要删除的机构组");return;}
		if (!showQuestion("是否删除这些机构组？")) return;
		DWR.call("partyService.deleteOrgGroups",ids,function(){
			showInformation("机构组删除成功");
			qt.showTable();
		},{blockScreen:true})
		
	}
	function back(){
		sg = {dataMap : {},adds : {},dels : {}}
		switchPage("listview");
		qt.showTable();
	}
	function searchSelOrg(){
		SystemMgnt.selOrg(function(sorg) {
				searchForm.orgName.value = sorg.name;
			},{funcType : Constants.SELORG_ALL});
	}
	</script>
</head>
<body onload="load()">
<!--页面导航-->
<div class="nav">
<div id="nav">&nbsp;</div>
</div>
<!--查询静态页面部分-->
<div id="listview"><!--查询条件form-->
<div class="searchForm">
<form id="searchForm" name="searchForm">
<table cellSpacing="0" cellPadding="4" width="100%" border="0">
	<tr>
		<td>
			<label>所属机构&nbsp;&nbsp;</label>
			<input type="text" name="orgName" maxlength="20" readonly class="readonly"><a class="orga" href="#" onclick="searchSelOrg();">&nbsp;&nbsp;</a>
		</td>
		<td><label>组名称</label> <input type="text" name="name" maxlength="32"></td>
		<td><label>组编号</label> <input type="text" name="uniqueId" maxlength="5"></td>
		<td>
		<button type="button" onclick="showData()">查 询</button>
		</td>
	</tr>
</table>
</form>
</div>
<div id=datalist></div>
</div>
<div id="detview" style="display: none;">
<div class="form">
<div class="formTitle">机构组信息</div>
<form id="orgGroupForm" name="orgGroupForm">
	<input type="hidden" name="id">
	<table cellSpacing="0" cellPadding="4" width="100%" border="0" class="label6">
		<tr>
			<td><label>机构组名称</label> <input type="text" name="name" maxlength="32" onblur="checkV(this,'',true)"> <span class="des">*</span></td>
			<td><label>机构等级</label> <select name="orgLevel" onblur="checkV(this,'',true)"></select> <span class="des">*</span></td>
		</tr>
	</table>
</form>
</div>
<div id="orggroupdiv"></div>
<p>
	<button onclick="saveOrgGroup()">保 存</button>
	<button onclick="back()">返 回</button>
</p>
</div>
<div id="detviewdiv" style="display: none;">
	<div class="form">
		<div class="formTitle">机构组信息</div>
		<table cellSpacing="0" cellPadding="4" width="100%" border="0" class="viewTable">
			<tr>
				<td><label>机构组名称</label> <span id="orgGroupName"></span></td>
				<td><label>机构等级</label> <span id="orgLevelText"></span></td>
			</tr>
			<tr>
				<td colspan="2"><label>成员机构</label> <span id="memberNames"></span></td>
			</tr>
		</table>
	</div>
	<p>
		<button onclick="switchPage('listview');">返 回</button>
	</p>
</div>
</body>
</html>